package net.suncaper.mvc.service.impl;

import net.suncaper.mvc.exception.BusinessException;
import net.suncaper.mvc.mapper.RoomMapper;
import net.suncaper.mvc.mapper.impl.RoomMapperImpl;
import net.suncaper.mvc.model.domain.Room;
import net.suncaper.mvc.service.IRoomService;

import java.math.BigDecimal;
import java.util.List;

/**
 * 房间业务接口实现类
 */
public class RoomServiceImpl implements IRoomService {
    // 依赖注入RoomMapper
    private final RoomMapper roomMapper = new RoomMapperImpl();

    /**
     * 添加房间
     */
    @Override
    public boolean addRoom(Room room) {
        // 校验必填字段
        if (room.getRoomType() == null || room.getRoomType().trim().isEmpty()) {
            throw new BusinessException("房型不能为空");
        }
        if (room.getHotelName() == null || room.getHotelName().trim().isEmpty()) {
            throw new BusinessException("酒店名称不能为空");
        }
        if (room.getPrice() == null || room.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException("房间价格必须大于0");
        }
        if (room.getAvailableCount() == null || room.getAvailableCount() < 0) {
            throw new BusinessException("可用房间数量不能为负数");
        }

        // 校验房间是否已存在（同一酒店的房型唯一）
        Room existingRoom = roomMapper.selectRoomByTypeAndHotel(room.getRoomType(), room.getHotelName());
        if (existingRoom != null) {
            throw new BusinessException("该酒店已存在【" + room.getRoomType() + "】房型");
        }

        return roomMapper.insertRoom(room);
    }

    /**
     * 根据酒店名称查询房间
     */
    @Override
    public List<Room> getRoomsByHotel(String hotelName) {
        if (hotelName == null || hotelName.trim().isEmpty()) {
            throw new BusinessException("酒店名称不能为空");
        }
        return roomMapper.selectRoomsByHotel(hotelName);
    }

    /**
     * 根据房型和酒店名称查询房间
     */
    @Override
    public Room getRoomByTypeAndHotel(String roomType, String hotelName) {
        if (roomType == null || roomType.trim().isEmpty()) {
            throw new BusinessException("房型不能为空");
        }
        if (hotelName == null || hotelName.trim().isEmpty()) {
            throw new BusinessException("酒店名称不能为空");
        }
        return roomMapper.selectRoomByTypeAndHotel(roomType, hotelName);
    }

    /**
     * 查询所有房间
     */
    @Override
    public List<Room> getAllRooms() {
        return roomMapper.selectAllRooms();
    }

    /**
     * 更新房间信息
     */
    @Override
    public boolean updateRoom(Room room) {
        // 校验必填定位字段
        if (room.getRoomType() == null || room.getRoomType().trim().isEmpty()) {
            throw new BusinessException("房型不能为空");
        }
        if (room.getHotelName() == null || room.getHotelName().trim().isEmpty()) {
            throw new BusinessException("酒店名称不能为空");
        }

        // 校验房间是否存在
        Room existingRoom = roomMapper.selectRoomByTypeAndHotel(room.getRoomType(), room.getHotelName());
        if (existingRoom == null) {
            throw new BusinessException("该酒店不存在【" + room.getRoomType() + "】房型，无法更新");
        }

        // 校验库存和价格（若有更新）
        if (room.getAvailableCount() != null && room.getAvailableCount() < 0) {
            throw new BusinessException("可用房间数量不能为负数");
        }
        if (room.getPrice() != null && room.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException("房间价格必须大于0");
        }

        return roomMapper.updateRoom(room);
    }

    /**
     * 删除房间
     */
    @Override
    public boolean deleteRoom(String roomType, String hotelName) {
        if (roomType == null || roomType.trim().isEmpty()) {
            throw new BusinessException("房型不能为空");
        }
        if (hotelName == null || hotelName.trim().isEmpty()) {
            throw new BusinessException("酒店名称不能为空");
        }

        // 校验房间是否存在
        Room existingRoom = roomMapper.selectRoomByTypeAndHotel(roomType, hotelName);
        if (existingRoom == null) {
            throw new BusinessException("该酒店不存在【" + roomType + "】房型，无法删除");
        }

        return roomMapper.deleteRoom(roomType, hotelName);
    }

    /**
     * 调整房间价格
     */
    @Override
    public boolean adjustRoomPrice(String roomType, String hotelName, BigDecimal newPrice) {
        // 校验参数
        if (roomType == null || roomType.trim().isEmpty()) {
            throw new BusinessException("房型不能为空");
        }
        if (hotelName == null || hotelName.trim().isEmpty()) {
            throw new BusinessException("酒店名称不能为空");
        }
        if (newPrice == null || newPrice.compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException("新价格必须大于0");
        }

        // 构建更新对象（仅更新价格）
        Room room = new Room();
        room.setRoomType(roomType);
        room.setHotelName(hotelName);
        room.setPrice(newPrice);

        return updateRoom(room); // 复用updateRoom方法
    }

    /**
     * 补充房间库存
     */
    @Override
    public boolean addRoomStock(String roomType, String hotelName, int addCount) {
        // 校验参数
        if (roomType == null || roomType.trim().isEmpty()) {
            throw new BusinessException("房型不能为空");
        }
        if (hotelName == null || hotelName.trim().isEmpty()) {
            throw new BusinessException("酒店名称不能为空");
        }
        if (addCount <= 0) {
            throw new BusinessException("补充数量必须大于0");
        }

        // 调用mapper层增加库存（复用现有方法）
        return roomMapper.increaseAvailableCountByOne(roomType, hotelName);
    }
}